from pyspades.serverloaders import PositionData
from pyspades.server import grenade_packet, block_action
from pyspades.collision import distance_3d_vector
from commands import add, admin
from map import Map
import random
import commands

@admin
def explosive(connection):
    if connection.protocol.explosive:
        connection.protocol.send_chat('Explosive rounds are now off.')
        connection.protocol.explosive = False
    else:
        if connection.protocol.explosive is None:
            connection.protocol.explosive = set()
        connection.protocol.send_chat('Explosive rounds are now on.')
        connection.protocol.explosive = True
add(explosive)

def apply_script(protocol, connection, config):
    class explosiveConnection(connection):  
        def on_block_removed(self, x, y, z):
            map = self.protocol.map
            if self.protocol.explosive == True and self.tool == 3:
                for nade_x in xrange(x - 1, x + 2):
                    for nade_y in xrange(y - 1, y + 2):
                        for nade_z in xrange(z - 1, z + 2):
                            map.remove_point(nade_x, nade_y, nade_z)
                block_action.x = x
                block_action.y = y
                block_action.z = z
                block_action.value = 3
                block_action.player_id = self.player_id
                self.protocol.send_contained(block_action, save = True)
            return connection.on_block_removed(self, x, y, z)
    class explosiveProtocol(protocol):
        explosive = None
    return explosiveProtocol, explosiveConnection